package service

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"math/rand"
	"net/http"
	"reggie/bean"
	"reggie/common"
	"reggie/dao"
	"strconv"
	"time"
)

func EmployeeLoginService(c *gin.Context)   {

	var employee bean.Employee

	if err := c.ShouldBindJSON(&employee); err != nil {
		c.JSON(400,gin.H{
			"code": "0",
			"msg": "param error",
			"data": nil,
		})
	}

	username := employee.Username

	find, _:= dao.EmployeeDaoFind(username)
	//fmt.Println("service find = ", find)
	u := find.Username
	p := find.Password

	// 写死 password： e10adc3949ba59abbe56e057f20f883e
	password := "e10adc3949ba59abbe56e057f20f883e"

	if p == password && u == username {

		// 生成用户id
		rand.Seed(time.Now().UnixNano())
		x := rand.Intn(100000000)   //生成随机整数
		s := strconv.Itoa(x)
		//fmt.Println("random x == ", x, s)

		// set baseContext
		common.SetBaseContext(s)

		// 给客户端设置cookie
		c.SetCookie("id", s, 3600, "/",
			"", false, true)

		// 返回
		c.JSON(http.StatusOK, gin.H{
			"data": find,
			"code": 1,
			"msg": "login success",
		})
	} else {
		c.JSON(http.StatusBadRequest, gin.H{
			"data": nil,
			"code": 0,
			"msg": "login fail",
		})
	}

}

func EmployeeLogoutService(c *gin.Context)  {
	// 给客户端设置cookie
	c.SetCookie("id", "", -1, "/",
		"", false, true)
	// 清除baseContext
	common.SetBaseContext("")

	c.JSON(http.StatusOK, gin.H{
		"code": 1,
		"msg": "success",
		"data": nil,
	})
}

func EmployeePageService(c *gin.Context)  {

	// 获取参数
	page := c.Query("page")
	pageSize := c.Query("pageSize")
	name := c.Query("name")

	// 入参校验：非空判断
	if page == "" || len(page) == 0 || pageSize == "" || len(pageSize) == 0 {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 0,
			"msg": "param error",
			"data": nil,
		})
		return
	}

	// 入参校验：格式校验
	iPage, err2 := strconv.Atoi(page)
	iPageSize, err3 := strconv.Atoi(pageSize)
	if err2 != nil && err3 != nil {
		c.JSON(http.StatusBadRequest, bean.Result{
			Code: 0,
			Msg: "param should be integer",
			Data: nil,
		})
		return
	}

	// 查数据
	iPage = (iPage - 1) * iPageSize
	Records, err := dao.EmployeeDaoPage(iPage, iPageSize, name)
	// 查总数
	total, err2 := dao.EmployeeDaoTotal(name)

	if err != nil && err2 != nil {
		fmt.Println("page err = ", err)
	}
	//fmt.Println("page size = ", len(list))
	//fmt.Println("page = ", list)

	fmt.Println("EmployeePageService.....")
	c.JSON(200, gin.H{
		"code": "1",
		"data": bean.Page{
			Total: total,
			Size: iPageSize,
			Records: Records,
			Current: iPage,
		},
		"msg": "ok",
	})
}

func EmployeeFindById(c *gin.Context)  {
	id := c.Param("id")
	fmt.Println("id = ", id)

	condition := make(map[string]interface{},1)
	condition["id"] = id
	conditions, err := dao.EmployeeDaoFindByConditions(condition)
	//fmt.Println("conditions = ", conditions)

	if err != nil {
		c.JSON(http.StatusInternalServerError, bean.Result{
			Code: 0,
			Msg: "internal server error",
			Data: nil,
		})
	}
	c.JSON(http.StatusOK, bean.Result{
		Code: 1,
		Msg: "success",
		Data: conditions,
	})
}

func EmployeeUpdateByConditions(c *gin.Context)  {
	var employee bean.Employee
	c.ShouldBindJSON(&employee)
	fmt.Println("service employee = ", employee)

	if err := dao.EmployeeDaoUpdateByConditions(employee); err != nil {
		c.JSON(http.StatusInternalServerError, bean.Result{
			Code: 0,
			Msg: "fail",
			Data: nil,
		})
	}
	c.JSON(http.StatusOK, bean.Result{
		Code: 1,
		Msg: "success",
		Data: nil,
	})

}
